﻿using System;
using System.Collections.Generic;
using System.Linq;
using Medianamik.Core.DAL;
using System.Web.Security;

namespace Medianamik.Core.Logging
{
    /// <summary>
    /// Acts as an interface between a DAL Provider and the UI for obtaining log entries
    /// </summary>
    public static class LogEntryManager
    {
        private static IConfigDALProvider Provider
        {
            get
            {
                return DALProviderManager.DALProvider.ConfigDALProvider ;
            }
        }

        public static List<ILogEntry> GetLogEntries(int pageIndex, int count)
        {
             return GetLogEntries(pageIndex, count, null);
        }

        public static List<ILogEntry> GetLogEntries(int pageIndex, int count, Dictionary<string, object> condition)
        {
            return Provider.GetLogEntries(pageIndex, count, condition);
        }

        public static int CountLogEntries()
        {
            return CountLogEntries(null);
        }

        public static int CountLogEntries(IDictionary<string, object> condition)
        {
            return Provider.CountLogEntries(condition);
        }

        public static void PurgeLogEntries()
        {
            PurgeLogEntries(null);   
        }

        public static void PurgeLogEntries(IDictionary<string, object> condition)
        {
            Provider.PurgeLogEntries(condition);
        }

        public static string GetPathForLog(ILogEntry log)
        {
            switch (log.Type)
            {
                case LogEntryType.Node:
                    return log.ObjectID.HasValue ? GetPathToNode(log.ObjectID.Value) : string.Empty;
                case LogEntryType.Template:
                    return "/medianamik/ContentManagement/TemplateList.aspx";
                case LogEntryType.User:
                    return "/medianamik/SecurityManagement/UserList.aspx";
                case LogEntryType.Role:
                    return "/medianamik/SecurityManagement/RoleList.aspx";
                case LogEntryType.Group:
                    return "/medianamik/SecurityManagement/GroupList.aspx";
                case LogEntryType.Redirect:
                    return "/medianamik/RedirectManagement/Default.aspx";
            }
            return string.Empty;
        }

        private static string GetPathToNode(Guid id)
        {
            var node = NodeManager.GetNode(id);
            if (node == null)
                return null;
            if (node.HasParentOfType(MedianamikTypes.BaseContentType))
            {
                return "/medianamik/contentmanagement/NodeList.aspx?" + DefaultParamNames.ParentNodeID + "=" + node.ParentNode.ID;
            }
                
            return string.Empty;
        }

        public static void LogUser(MembershipUser user, string message)
        {
            var medUser = user as Security.MedianamikUser;
            if (medUser != null)
            {
                var logger = new ActivityLogger();
                logger.Log(medUser, message);
            }
        }
        
        static bool HasParentOfType(this Node node, Guid typeId)
        {
            if (node == null || node.ParentNode == null)
                return false;

            List<Guid> parentIds = node.NodeType.GetParentIds(true);
            return parentIds.Any(i => i == typeId);
        }
    }
}
